package cn.ekuma.data.ui.android.editor;

import com.openbravo.data.user.I_AndroidBrowsableEditableData;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.bean.I_ViewBean;
import cn.ekuma.data.ui.android.AbstractFinderActivate;
import cn.ekuma.data.ui.android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.PopupMenu;

public abstract class AbstractEditorActivate<T> extends Activity  {
	protected AbstractDAOJEditor<T> editor;
    protected I_DataLogic dlSales;
    protected DirtyManager dirty;
    protected I_AndroidBrowsableEditableData m_db;
    protected JNavigator navigator;
    protected JSaver saver;
    LinearLayout jContext;
    
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	dlSales=getDataLogic();
		dirty=new DirtyManager();
		initActivate(savedInstanceState);
		m_db=getBrowsableEditableData();
		setContentView(R.layout.editor);
		editor=getEditor();
		jContext=(LinearLayout) this.findViewById(R.id.jContext);
			
		navigator=(JNavigator) this.findViewById(R.id.jNavigator);
		navigator.setAndroidBrowsableEditableData(m_db);
		saver=new JSaver(m_db);
		jContext.addView(editor);
		//try {
			//m_db.moveTo(0);
		//} catch (BasicException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
    }
    
   protected  void  initActivate(Bundle savedInstanceState){
	   
   }

    /**添加ActionBar的Menu*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_edit, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**添加Menu的下拉子菜单
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
            case R.id.action_manage:
                if(mManageMenu == null)
                {
                    mManageMenu = new PopupMenu(mContext,
                            findViewById(R.id.action_manage));
                    mManageMenu.inflate(R.menu.main_item_manage);
                    mManageMenu.setOnMenuItemClickListener(this);
                }
                mManageMenu.show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }*/

    /**监听子菜单的点击事件*/
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int id=menuItem.getItemId();
         if(id== R.id.action_new) {
             saver.jbtnNewActionPerformed();
         }else if(id== R.id.action_save) {
             saver.jbtnSaveActionPerformed();
         } else if( id==R.id.action_del) {
             saver.jbtnDeleteActionPerformed();
         }
        return false;
    }

    protected abstract I_AndroidBrowsableEditableData getBrowsableEditableData();

   protected abstract I_DataLogic getDataLogic();

   protected abstract AbstractDAOJEditor<T> getEditor();


@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  if(resultCode==AbstractFinderActivate.MSG_SUCCESS){
		  I_ViewBean obj=(I_ViewBean) data.getSerializableExtra(AbstractFinderActivate.SELECTED_BEAN);
		  selectObject(requestCode,obj);
	  }else
		  selectObject(requestCode,null);
   }
   
  protected  void selectObject(int requestCode,I_ViewBean obj){

  }
}
